// Copyright 2008 Silvio Massari
//
// This file is part of SilvioMassari.Automata
//
// SilvioMassari.Automata is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// SilvioMassari.Automata is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with SilvioMassari.Automata.  If not, see <http://www.gnu.org/licenses/>.
				
using System;
using System.Collections.Generic;
using System.Text;

namespace SilvioMassari.Automata
{
	/// <summary>
	/// 
	/// </summary>
	/// <typeparam name="T">Type of elements to process</typeparam>
	public class State<S, T>
	{
		public State()
		{
		}

		private T _value;
		private bool _finalization = false;
		private List<State<S, T>> _nextByNull = new List<State<S, T>>();
		private Dictionary<S, State<S, T>> _nextByChar = new Dictionary<S, State<S, T>>();

		public Dictionary<S, State<S, T>> NextByChar
		{
			get { return _nextByChar; }
			set { _nextByChar = value; }
		}

		public List<State<S, T>> NextByNull
		{
			get { return _nextByNull; }
			set { _nextByNull = value; }
		}

		public T Value
		{
			get { return _value; }
			set { _value = value; }
		}

		public bool Finalization
		{
			get { return _finalization; }
			set { _finalization = value; }
		}

		public void MoveNull(List<State<S, T>> states)
		{
            if (!states.Contains(this))
			{
				states.Add(this);
				foreach (State<S, T> state in _nextByNull)
				{
					state.MoveNull(states);
				}
			}
		}

		public void Move(List<State<S, T>> states, S element)
		{
            if (!states.Contains(this) && _nextByChar.ContainsKey(element))
			{
                State<S, T> next = _nextByChar[element];
                if (next != null && !states.Contains(next))
                {
                    next.MoveNull(states);
                }
            }
		}
	}
}
